package com.rc.evcharger.client;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rc.evcharger.common.base.TResponse;
import com.rc.evcharger.common.config.RouterConfig;
import com.rc.evcharger.model.elec.ElecPileHost;
import com.rc.evcharger.service.elec.ElecPileHostService;
import com.rc.evcharger.vo.charge.StartChargeReq;
import com.rc.evcharger.vo.charge.StopChargeReq;
import com.rc.evcharger.utils.RestTemplateUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

/**
 * 多品牌充电服务器域名,STD_DOMAIN,ZC_DOMAIN
 * Created by sven on 2019/12/13
 */
@Component
public class ChargerClientRouter {
    private static Logger logger = LogManager.getLogger(ChargerClientRouter.class);

    @Autowired
    private ElecPileHostService elecPileHostService;

    @Autowired
    private RestTemplateUtil restTemplateUtil;

    /**
     * 下达开始充电指令
     *
     * @param tRequest
     * @return
     */
    public TResponse query_start_charge(StartChargeReq startChargeReq) {
        //查地址表
        ElecPileHost elecPileHost = elecPileHostService.selectByPrimaryKey(startChargeReq.getElecPileId());

        //拼接eureka访问地址
        String requestUrl = RouterConfig.STD_START_CHARGE_PATH;
        if (elecPileHost.getApplicationName().contains("std")) {
            requestUrl = "http://" + elecPileHost.getApplicationName() + RouterConfig.STD_SERVICE_PATH + RouterConfig.STD_START_CHARGE_PATH;
        } else {
            requestUrl = "http://" + elecPileHost.getApplicationName() + RouterConfig.MUL_SERVICE_PATH + RouterConfig.STD_START_CHARGE_PATH;
        }

        ObjectMapper objectMapper = new ObjectMapper();
        String respData = null;
        try {
            respData = objectMapper.writeValueAsString(startChargeReq);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        JSONObject tRequestJson = JSONObject.parseObject(respData);
        try {
            logger.info("std入参加密接口,启动充电路由被调用，...............对应url:{},orderSource:{}", requestUrl);
            //通过rest调用远程服务接口
            ResponseEntity<TResponse> request = restTemplateUtil.request(requestUrl, HttpMethod.POST, tRequestJson, TResponse.class);
            logger.info("std入参加密接口,启动充电路由被调用，...............返回状态码:{},结果集:{}", request.getStatusCode(), request.getBody().toString());
            return request.getBody();
        }catch (Exception e){
            logger.info("std入参加密接口,启动充电路由被调用失败，...............对应url:{},orderSource:{}", requestUrl);
            e.printStackTrace();
            return TResponse.FAIL("请求失败");
        }

    }


    /**
     * 下达结束充电指令
     * @return
     */
    public TResponse query_stop_charge(StopChargeReq stopChargeReq) {
        //查地址表
        ElecPileHost elecPileHost = elecPileHostService.selectByPrimaryKey(stopChargeReq.getElecPileId());

        //拼接eureka访问地址
        String requestUrl = RouterConfig.STD_START_CHARGE_PATH;
        if (elecPileHost.getApplicationName().contains("std")) {
            requestUrl = "http://" + elecPileHost.getApplicationName() + RouterConfig.STD_SERVICE_PATH + RouterConfig.STD_STOP_CHARGE_PATH;
        } else {
            requestUrl = "http://" + elecPileHost.getApplicationName() + RouterConfig.MUL_SERVICE_PATH + RouterConfig.STD_STOP_CHARGE_PATH;
        }

        ObjectMapper objectMapper = new ObjectMapper();
        String respData = null;
        try {
            respData = objectMapper.writeValueAsString(stopChargeReq);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        JSONObject tRequestJson = JSONObject.parseObject(respData);

        try {
            logger.info("std入参加密接口,停止充电路由被调用，...............对应url:{},orderSource:{}", requestUrl);
            //通过rest调用远程服务接口
            ResponseEntity<TResponse> request = restTemplateUtil.request(requestUrl, HttpMethod.POST, tRequestJson, TResponse.class);
            logger.info("std入参加密接口,停止充电路由被调用，...............返回状态码:{},结果集:{}", request.getStatusCode(), request.getBody().toString());
            return request.getBody();
        } catch (Exception e) {
            logger.info("std入参加密接口,停止充电路由被调用失败，...............对应url:{},orderSource:{}", requestUrl);
            e.printStackTrace();
            return TResponse.FAIL("请求失败");
        }

    }
}
